package com.kwq.designPattern.exam;

import java.util.*;

/**
 * @author kangwanqiang
 * @date 2021/3/7
 * @description 字节跳动春招笔试题
 */
public class ByteDance {
    public static String getDecodeStr(String encodeStr) {
        String[] strs = new String[26];
        char start = encodeStr.charAt(0);
        int steps = 0;
        if (start >= 'A' && start <= 'Z') {
            steps = start - 'A' + 1;
        } else if (start >= '0' && start <= '9') {
            steps = start - '0';
        } else {
            steps = 1;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (char c : encodeStr.toCharArray()) {
            if (c >= 'A' && c <= 'Z') {
                stringBuilder.append((char) (((c - 'A' + steps) % 26) + 65));
            } else if (c >= '0' && c <= '9') {
                stringBuilder.append( (Integer.parseInt(String.valueOf(c)) + steps) % 10);
            } else {
                stringBuilder.append(c);
            }
        }
        return stringBuilder.toString();
    }

    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] persons = new int[n];
        for (int i = 0; i < n; i++) {
            persons[i] = scanner.nextInt();
        }
        Arrays.sort(persons);
        int left = persons[n - 1];
        int right = persons[n - 1];
        int appendDir = 1;
        int maxSub = 0;
        for (int i = n - 2; i >= 0; i--) {
            if (appendDir == 1) {
                int sub = right - persons[i];
                maxSub = Math.max(maxSub, sub);
                right = persons[i];
                appendDir = 2;
            } else {
                int sub = left - persons[i];
                maxSub = Math.max(maxSub, sub);
                left = persons[i];
                appendDir = 1;
            }
        }
        maxSub = Math.max(maxSub, Math.abs(right - left));
        System.out.println(maxSub);
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.nextLine();
        String s2 = scanner.nextLine();
        char[] chars1 = s1.toCharArray();
        char[] chars2 = s2.toCharArray();
         int[][] diff = new int[s1.length()][s2.length()];
        for (int i = 0; i < s1.length(); i++) {
            Set<Character> set = new HashSet<>();
            for (int i1 = 0; i1 <= i; i1++) {
                set.add(chars1[i1]);
            }
            for (int j = 0; j < s2.length(); j++) {
                set.add(chars2[j]);
                diff[i][j] = set.size();
            }
        }
        int count = scanner.nextInt();
        for (int i = 0; i < count; i++) {
            System.out.println(diff[scanner.nextInt()-1][scanner.nextInt()-1]);
        }
    }
}
